using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class XemThongTinGioHang : System.Web.UI.Page
{
    ShoppingCartBO shoppingBO = new ShoppingCartBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        grdProducts.RowEditing += new GridViewEditEventHandler(grdProducts_RowEditing);
       // if (!IsPostBack)
       // {
            BinDataToGrid();
       // }
    }

    void grdProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        TextBox txtSoLuong = new TextBox();
        Label maSp = new Label();        
        txtSoLuong = (TextBox)grdProducts.Rows[e.NewEditIndex].FindControl("txtSoLuong");
        maSp = (Label)grdProducts.Rows[e.NewEditIndex].FindControl("lblBasketId");
        
        DataTable table = (DataTable)Session["tableGioHang"];
        DataRow[] Dong = table.Select("MaSanPham = " + maSp.Text.Trim());
        if (Dong.Length > 0)
        {
            try
            {
                int sl = Int16.Parse(txtSoLuong.Text.Trim());
                if (sl >= 0)
                {
                    Dong[0]["SoLuong"] = sl;
                }

            }
            catch { }
            Session["tableGioHang"] = table;
            // BinDataToGrid();
        }
    }
    public void BinDataToGrid()
    {
        DataTable table = (DataTable)Session["tableGioHang"];// shoppingBO.GetAllShoppingCart();

        ViewState["ShoppingCart"] = table;
        grdProducts.DataSource = table;
        grdProducts.DataBind();
    }

    private DataTable ShoppingCartList
    {
        get
        {
            DataTable table = ViewState["ShoppingCart"] as DataTable;
            if (table != null)
            {
                return table;
            }
            else
            {
                return new DataTable();
            }
        }
    }


    protected void grdProducts_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grdProducts.PageIndex = e.NewSelectedIndex;
        grdProducts.DataSource = ShoppingCartList;
        grdProducts.DataBind();
    }
    protected void grdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("xoa"))
        {
            int id = Convert.ToInt32(e.CommandArgument);
            DataTable table = (DataTable)Session["tableGioHang"];
            if (table == null)
                return;
            DataRow[] dongDelete = table.Select("MaSanPham = " + id);
            if (dongDelete.Length > 0)
            {
                dongDelete[0].Delete();
                Session["tableGioHang"] = table;
                BinDataToGrid();
            }
        }
        else if (e.CommandName.Equals("capnhat"))
        {
            TextBox soluong = new TextBox();

           // grdProducts.Rows[e.RowIndex].FindControl("txtQty");
           

        }
    }
    protected void btnTiepTucMua_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        Session["tableGioHang"] = null;        

    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        //xu ly luu don hang
    }
    protected void btnThanhToan_Click(object sender, EventArgs e)
    {
        //xu ly thanh toan
    }

    protected void grdProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        TextBox txtSoLuong = new TextBox();
        Label maSp = new Label();
        txtSoLuong = (TextBox)grdProducts.Rows[e.RowIndex].FindControl("txtSoLuong");
        maSp = (Label)grdProducts.Rows[e.RowIndex].FindControl("lblBasketId");
        
        DataTable table = (DataTable)Session["tableGioHang"];
        DataRow[] Dong = table.Select("MaSanPham = " + maSp.Text.Trim());
        if (Dong.Length > 0)
        {
            try
            {
                int sl =  Int16.Parse(txtSoLuong.Text.Trim());
                if(sl >= 0)
                {
                    Dong[0]["SoLuong"] = 0;
                }
                
            }
            catch { }
            Session["tableGioHang"] = table;
            BinDataToGrid();
        }
    }
    //protected void lnkUpdate_Click(object sender, EventArgs e)
    //{
    //    GridViewRow row = grdProducts.SelectedRow;

    //    TextBox txtSoLuong = new TextBox();
    //    Label maSp = new Label();
    //    txtSoLuong = (TextBox)row.FindControl("txtSoLuong");
    //    maSp = (Label)row.FindControl("lblBasketId");

    //    DataTable table = (DataTable)Session["tableGioHang"];
    //    DataRow[] Dong = table.Select("MaSanPham = " + maSp.Text.Trim());
    //    if (Dong.Length > 0)
    //    {
    //        try
    //        {
    //            int sl = Int16.Parse(txtSoLuong.Text.Trim());
    //            if (sl >= 0)
    //            {
    //                Dong[0]["SoLuong"] = 0;
    //            }

    //        }
    //        catch { }
    //        Session["tableGioHang"] = table;
    //        BinDataToGrid();
    //    }

    //}
}
